<?php

/**
 * 现场签证控制器
 */

namespace Admin\Controller;

use Common\Controller\AuditbaseController;

class SiteVisaController extends AuditbaseController {

    public function _initialize() {
        parent::_initialize();
        $this->deal_model = D("Common/SiteVisa");

        $this->check = array('code');

        // 当前审批流程
        $this->function = "contract";
        $this->subfunction = "6";
    }

    /**
     * 主页
     */
    public function index() {
        /*         * 搜索条件* */
        $where = "d.is_deleted <> 1 ";
        $this->indexAuditPage($where);
    }

    /**
     *  删除 
     */
    public function delete(){
        $id = I('get.id', 0, 'intval');
        if(!$id){
            $this->error("请选择要删除的数据！");
        }
        $data = array(
            'id'    => $id,
            'is_deleted' => 1
        );
        if ($this->deal_model->save($data) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    // 保存新建审批表单
    /*public function add_post(){
        if(IS_POST){
            $url = U(CONTROLLER_NAME.'/'.(I('get.fun') != NULL ? I('get.fun') : 'index'));
            $this->dataCheck();
            $data = array_merge($_POST, $this->saveData());
            if ($this->deal_model->create()!==false) {
                $result = $this->deal_model->add($data);
                if ($result!==false) {
                    // 创建审批 一级流程数据
                    $history = array(
                        'table_id'          => $result,
                        'audit_status'      => 1,
                        'audit_reason'      => '新建表单',
                        'amount_in_words'   => '',
                        'audit_level'       => 1
                    );
                    $data = array_merge($history, $this->auditAdd());
                    $this->auditHistoryModel->add($data);
                    $this->saveFile($result, 16);
                    
                    $this->success("添加成功！", $url);
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
    }

    // 跳转查看画面
    public function view(){
        // 获取基础数据
        $id = I('get.id',0,'intval');
        $result = $this->deal_model->where(array("id"=>$id))->find();
        $results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>16))->select();
        $this->assign("result", $result);
        $this->assign("results", $results);
                               
        $isAudit = '1';
        // 验证当前登录人是否有审批权限
        if($result['audit_level'] == 2 && $result['add_department'] == session('deptid') && session('Role') == 3){
            $isAudit = '2';
        } elseif($result['audit_level'] > 2) {
            // 查询当前审批部门
            $auditdept = $this->auditFlowModel->alias("a")
                            ->field("d.department_id")
                            ->join(array(
                                ' LEFT JOIN '.C('DB_PREFIX').'audit_flow_dept d ON d.flow_id = a.id'
                            ))
                            ->where(array(
                                'a.function'    => $this->function,
                                'a.subfunction' => $this->subfunction,
                                'd.`level`'     => intval(($result['audit_level'] - 1)/2)
                            ))
                            ->find();
            if((intval($result['audit_level'])%2 == 1 && $auditdept['department_id'] == session("deptid") && session('Role') == 2)
                    OR(intval($result['audit_level'])%2 == 0 && $auditdept['department_id'] == session("deptid") && session('Role') == 3)){
                $isAudit = '2';
            }
        }
        $this->assign("isAudit", $isAudit);
        //dump($isAudit);
        // 循环查询审核意见
        $auditLevel = $this->getAuditFlowLevel();
        $auditResult = array();
        if($auditLevel > 2){
            for($i = 3; $i < $auditLevel; $i += 2){
                $auditResult[$i] = $this->auditHistoryModel->where(array('function' => $this->function, 'subfunction' => $this->subfunction, 'audit_level' => $i, 'table_id' => $id))->order("add_time DESC")->find();
            }
        }
        $this->assign("audit", $auditResult);
        //var_dump($auditResult);
        // 判断最终状态是否为驳回
        if($result['audit_status'] == '2'){
            // 查询驳回原因
            $returnMsg = $this->auditHistoryModel->where(array('function' => $this->function, 'subfunction' => $this->subfunction, 'table_id' => $id))->order("add_time DESC")->find();
            $this->assign("reject", $returnMsg);
        }
        
        // 验证审批权限
        $this->display();
    }

    // 获取当前审批流程的总审核层级
    private function getAuditFlowLevel(){
        $result = $this->auditFlowModel
                ->where(array(
                    'function'      => $this->function,
                    'subfunction'   => $this->subfunction,
                ))
                ->getField("level", true);
        return (intval(($result[0] != '' ? $result[0] : 0)) + 1) * 2;
    }*/
	//分配权限页面
    public function distribution(){
        $level = $_GET['level'];
        //var_dump($level);
        $this->assign("level", $level);
        $this->distribution_base();
    }

}

?>